'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Net
Imports System.Windows.Forms

Public Class G_node
    Public ISPReturnData As New ISPReturnData
    Private CookieContainer As New System.Net.CookieContainer

    Public Sub New(ByVal ConfigurationData As ConfigurationData)

        Try
            'set rollover date
            SetRolloverDate(ConfigurationData, ISPReturnData)

            Dim strHTMLData As String

            'login
            Dim POSTData As String = "username=" & ConfigurationData.Username & "&password=" & ConfigurationData.Password
            strHTMLData = ISPPost("https://www.g-node.com.au/login.asp", POSTData, CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            If strHTMLData.Contains("Incorrect username or password") = True Then
                ISPReturnData.ErrorMessage = "Login Failed"
                Exit Sub
            End If

            'account type
            Dim TempData As String = ISPGet("https://www.g-node.com.au/services.asp", CookieContainer, System.Net.CredentialCache.DefaultCredentials)
            Dim PlanType As String = LCase(ExtractText(TempData, "Plan:", 0, ">", "<"))
            If PlanType = "" Then Exit Sub
            Dim RecordID As String = ExtractText(TempData, "recordID", 0, "=", """")

            Dim strTemp As String
            'usage
            If PlanType.StartsWith("g") = True Then
                'strHTMLData = ISPGet("https://www.g-node.com.au/metering_gtime.asp?recordID=" & RecordID, CookieContainer, System.Net.CredentialCache.DefaultCredentials)
                strHTMLData = ISPGet("https://www.g-node.com.au/metering.asp?recordID=" & RecordID, CookieContainer, System.Net.CredentialCache.DefaultCredentials)

                If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

                'process response
                'strTemp = ExtractText(strHTMLData, "Monthly Total:", 0, """right"">", "B")
                strTemp = ExtractText(strHTMLData, "All Traffic:", 0, """right"">", "B")
                ISPReturnData.PeakUpload = CSng(Val(strTemp))
                If ISPReturnData.PeakUpload <> 0 Then
                    'value in GB
                    ISPReturnData.PeakUpload = CSng(Val(ISPReturnData.PeakUpload) * 1000)
                End If

                'strTemp = ExtractText(strHTMLData, "Monthly Total (excluding G-time):", 50, """right"">", "B")
                strTemp = ExtractText(strHTMLData, "<b>Off Net (Metered / Shaped):</b>", 50, """right""><b>", "B")
                ISPReturnData.PeakDownload = CSng(Val(strTemp))
                If ISPReturnData.PeakDownload <> 0 Then
                    'value in GB
                    ISPReturnData.PeakDownload = CSng(Val(ISPReturnData.PeakDownload) * 1000)
                    'ISPReturnData.PeakShaped = False
                    'ISPReturnData.OffpeakShaped = False
                    'If Val(ISPReturnData.PeakDownload) >= Val(ISPReturnData.PeakUsageLimit) Then
                    '    ISPReturnData.PeakShaped = True
                    '    ISPReturnData.OffpeakShaped = True
                    'End If
                End If

                'get offpeak d/l
                'strTemp = ExtractText(strHTMLData, "Monthly Total:", 50, """right"">", "B")
                strTemp = ExtractText(strHTMLData, "All Traffic:", 50, """right"">", "B")
                ISPReturnData.OffpeakDownload = CSng(Val(strTemp))
                If ISPReturnData.OffpeakDownload <> 0 Then
                    ISPReturnData.OffpeakDownload = CSng((Val(ISPReturnData.OffpeakDownload) * 1000))
                    ISPReturnData.OffpeakDownload = CSng(Val(ISPReturnData.OffpeakDownload) - Val(ISPReturnData.PeakDownload))
                End If

                ISPReturnData.LastUpdated = Now

                '##########################################################

                ProcessISPData(ConfigurationData, ISPReturnData)

                'check for error
                If ISPReturnData.ErrorMessage <> "" Then Exit Sub

                '##########################################################

                'create graph data

                'peak
                ISPReturnData.GraphItemList.Add(New GraphItem("Peak " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), ISPReturnData.PeakNow))

                'offpeak
                ISPReturnData.GraphItemList.Add(New GraphItem("Offpeak Unlimited (" & ISPReturnData.OffpeakDownload & " MB)", ISPReturnData.OffpeakDownload, 0, ISPReturnData.OffpeakNow))

                'days elapsed
                ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

                '##########################################################

                'create stats data
                ISPReturnData.TextItemList.Add(New TextItem("Peak Left :", ISPReturnData.PeakUsageRemainingDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Peak Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Peak U/L:", ISPReturnData.PeakUpload & " MB"))
                If ISPReturnData.ShapedNow = False Then
                    ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "NO"))
                Else
                    ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "YES"))
                End If

            Else
                strHTMLData = ISPGet("https://www.g-node.com.au/metering.asp?recordID=" & RecordID, CookieContainer, System.Net.CredentialCache.DefaultCredentials)

                If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

                'process response
                strTemp = ExtractText(strHTMLData, "Monthly Total:", 0, """right"">", "B")
                ISPReturnData.PeakUpload = CSng(Val(strTemp))
                If ISPReturnData.PeakUpload = 0 Then
                    If strTemp.EndsWith("G") = True Then ISPReturnData.PeakUpload = CSng((Val(ISPReturnData.PeakUpload) * 1000))
                End If

                strTemp = ExtractText(strHTMLData, "Monthly Total:", 50, """right"">", "B")
                ISPReturnData.PeakDownload = CSng(Val(strTemp))
                'ISPReturnData.PeakShaped = False
                'ISPReturnData.OffpeakShaped = False
                If ISPReturnData.PeakDownload = 0 Then
                    If strTemp.EndsWith("G") = True Then ISPReturnData.PeakDownload = CSng((Val(ISPReturnData.PeakDownload) * 1000))
                    'If Val(ISPReturnData.PeakDownload) >= Val(ISPReturnData.PeakUsageLimit) Then
                    '    ISPReturnData.PeakShaped = True
                    '    ISPReturnData.OffpeakShaped = True
                    'End If
                End If

                ISPReturnData.LastUpdated = Now

                '##########################################################

                ProcessISPData(ConfigurationData, ISPReturnData)

                'check for error
                If ISPReturnData.ErrorMessage <> "" Then Exit Sub

                '##########################################################

                'create graph data

                'peak
                ISPReturnData.GraphItemList.Add(New GraphItem("Peak " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), ISPReturnData.PeakNow))

                'offpeak
                ISPReturnData.GraphItemList.Add(New GraphItem("Offpeak Unlimited (" & ISPReturnData.OffpeakDownload & " MB)", ISPReturnData.OffpeakDownload, 0, ISPReturnData.OffpeakNow))

                'days elapsed
                ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

                '##########################################################

                'create stats data
                ISPReturnData.TextItemList.Add(New TextItem("Peak Left :", ISPReturnData.PeakUsageRemainingDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Peak Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Peak U/L:", ISPReturnData.PeakUpload & " MB"))
                If ISPReturnData.ShapedNow = False Then
                    ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "NO"))
                Else
                    ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "YES"))
                End If
            End If

            '##########################################################

            'logout
            ISPGet("https://www.g-node.com.au/logout.asp", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            '##########################################################

            'process alerts
            ProcessAlerts(ConfigurationData, ISPReturnData)

        Catch ex As Exception
            ISPReturnData.ErrorMessage = ex.Message
        End Try
    End Sub

End Class
